package cn.ld.fj.web.log;

import cn.ld.fj.entity.OperationLog;
import cn.ld.fj.entity.VipUser;
import cn.ld.fj.entity.account.User;
import cn.ld.fj.service.account.AccountManager;
import cn.ld.fj.service.log.OperationLogManager;
import cn.ld.fj.service.vip.VipUserManager;
import cn.ld.fj.util.DwzUtil;
import cn.ld.fj.web.JsonActionSupport;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import net.esoar.modules.utils.web.struts2.Struts2Utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 用户管理Action.
 * <p/>
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 *
 * @author fly
 */
// 定义URL映射对应/account/user.action
@Namespace("/log")
// 定义名为reload的result重定向到user.action, 其他result则按照convention默认.
@Results({@Result(name = JsonActionSupport.RELOAD, location = "log.action", type = "redirect")})
public class LogAction extends JsonActionSupport<OperationLog> {


    @Autowired
    private OperationLogManager operationLogManager;
    @Autowired
    private AccountManager accountManager;

    private Long id;
    private OperationLog entity;
    private Page<OperationLog> page = new Page<OperationLog>(10);// 每页5条记录


    public void setId(Long id) {
        this.id = id;
    }

    public void setEntity(OperationLog entity) {
        this.entity = entity;
    }

    public void setPage(Page<OperationLog> page) {
        this.page = page;
    }

    public Long getId() {
        return id;
    }

    public OperationLog getEntity() {
        return entity;
    }

    public Page<OperationLog> getPage() {
        return page;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = operationLogManager.getOne(id);
        } else {
            entity = new OperationLog();
        }
    }

    // -- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        List<PropertyFilter> filters = PropertyFilter
                .buildFromHttpRequest(Struts2Utils.getRequest());
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.DESC);
        }

        page = operationLogManager.getPage(page, filters);
        if (CollectionUtils.isNotEmpty(page.getResult())) {
            for (OperationLog operationLog : page.getResult()) {
                User user = accountManager.getUser(operationLog.getOperationId());
                if (user != null) {
                    operationLog.setUserName(user.getName());
                }

            }
        }

        return SUCCESS;
    }

    @Override
    public String input() throws Exception {

        return INPUT;
    }

    @Override
    public void save() throws Exception {
        operationLogManager.save(entity);
        Struts2Utils.renderHtml(DwzUtil.getCloseCurrentReturn("w_log",
                "操作成功"));
    }

    @Override
    public void delete() throws Exception {


        try {
            operationLogManager.delete(id);
        } catch (Exception e) {
            return;
        }

        Struts2Utils.renderHtml(DwzUtil.getNavtabReturn("w_log",
                "操作成功"));
    }

    @Override
    public OperationLog getModel() {
        return entity;
    }
}
